namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceOtherPageEdit : Page
    {
        protected Image iResource;
        protected Label lbOtherIntro;
        protected Label lbOtherSubject;
        protected TextBox OtherIntroTxb;
        protected TextBox OtherSubjectTxb;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["OtherId"]);
                ResourceOther resourceOther = ManageResource.GetResourceOtherInfoViaId(id);
                this.OtherSubjectTxb.Text = resourceOther.OtherSubject;
                this.OtherIntroTxb.Text = resourceOther.OtherIntro;
            }
        }

        private void InitializeComponent()
        {
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strOtherSubject = this.OtherSubjectTxb.Text.Trim();
            string strOtherIntro = this.OtherIntroTxb.Text;
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strOtherSubject != "")
                {
                    ManageResource.UpdateResourceOtherInfo(new Guid(base.Request.QueryString["OtherId"]), strOtherSubject, strOtherIntro);
                    base.Response.Redirect("~/Resource/ResourceOtherPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strOtherSubject != "")
            {
                if (!ManageResource.IsExistSameResourceOtherInfo(strOtherSubject))
                {
                    ManageResource.InsertNewResourceOtherInfo(strOtherSubject, strOtherIntro);
                    base.Response.Redirect("~/Resource/ResourceOtherPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbOtherSubject.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceOtherPageEdit", "lbOtherSubject", language, path);
            this.lbOtherIntro.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceOtherPageEdit", "lbOtherIntro", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceOtherPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

